<div class="row">
  <div class="col-md-12">
    <h2>Repository Monitoring</h2>
    <br>
  </div>
</div>
<% if current_user.private_repo_token.blank? %>
<div class="row">
  <div class="col-md-12">
    <div class="alert alert-success" role="alert">
      <%= fa_icon 'info-circle' %>
      Enable private repo tracking upgrading your GitHub OAuth access.
      <a href="<%= enable_private_path %>" class="btn btn-info btn-xs learn-more" data-dismiss="alert" aria-label="Close">Authorize Now</a>
    </div>
  </div>
</div>
<% end %>

<div class='row'>
  <% if current_user.needs_to_enable_github_access? %>
    <div class="col-md-8">
      <%= render 'monitoring_promo' %>
    </div>
  <% else %>
    <div class="col-md-3">
      <div class='org-filter <%= 'active' unless @org %>'>
        <%= link_to url_for(org: nil) do %>
          <%= image_tag current_user.avatar_url(60), width: 30, height: 30 %>
          <%= current_user.nickname %>
        <% end %>
      </div>
      <% @orgs.each do |org| %>
        <% next if org.blank? %>
        <div class='org-filter <%= 'active' if @org && org.login == @org.login %>'>
          <%= link_to url_for(org: org.login) do %>
            <%= image_tag org.avatar_url(60), width: 30, height: 30 %>
            <%= org.login %>
          <% end %>
        </div>
      <% end %>
      <p>
        <small>
        Is an organization missing?<br>
        <%= link_to 'Review and add', current_user.github_settings_url %> your authorized organizations.
        </small>
      </p>
      <div class='syncing'>
        <% if current_user.currently_syncing? %>
          <i class="fa fa-refresh fa-spin"></i> currently syncing
        <% else %>
          <%= link_to 'Sync', sync_path, method: :post, class: 'btn btn-sm btn-info' %>
          <% if current_user.last_synced_at.present? %>
            <small>Last updated <%= timeago_tag current_user.last_synced_at %></small>
          <% end %>
        <% end %>
      </div>
    </div>
    <div class="col-md-9">
      <table class='table repositories table-hover table-condensed'>
        <thead>
          <th colspan="2">
            <small class='pull-right'>
              <%= page_entries_info @repos, model: "#{@org.try(:login)} repositories" %>
            </small>
          </th>
        </thead>
        <% @repos.each do |repository| %>
          <tr>
            <td>
                <%= link_to image_tag(repository.avatar_url(40), width: 20, height: 20), repository_path(repository.to_param) %>
                <strong>
                  <%= link_to repository.full_name, repository_path(repository.to_param) %>
                </strong>
                <small>
                  <%= fa_icon("lock") if repository.private? %>
                  <%= fa_icon("code-fork") if repository.fork? %>
                </small>
            </td>
            <td>
              <% if sub = current_user.subscribed_to_repo?(repository) %>
                <div class="btn-group pull-right">
                  <%= link_to unwatch_path(repository.id), method: :post, class: 'tip btn btn-sm btn-success', title: 'Stop watching' do %>
                    <%= fa_icon('check') %>
                    Monitoring
                  <% end %>
                  <%= link_to fa_icon('cog'), edit_repository_subscription_path(sub), class: 'btn btn-sm btn-success' %>
                </div>
              <% else %>
                <%= button_to watch_path(repository.id), method: :post, class: 'btn btn-sm btn-default  pull-right' do %>
                  <%= fa_icon('eye') %>
                  Monitor
                <% end %>
              <% end %>
            </td>
          </tr>
        <% end %>
      </table>

      <%= will_paginate @repos, page_links: false %>
    </div>
  <% end %>
</div>
